
var bgSrc = new Image();
var bg;
var gfxLoaded = 0;
var stage;

function run()
{
	
	bgSrc.src = 'bg.png';
	bgSrc.name = 'bg';
	bgSrc.onload = loadGfx;
	
	Ticker.setFPS(30);
	Ticker.addListener(stage);
}

function loadGfx(e)
{
	if(e.target.name = 'bg'){bg = new Bitmap(bgSrc);}
	
	gfxLoaded++;
	
	/* Display graphics until all of them are loaded */
	
	if(gfxLoaded == 1)
	{
		buildInterface();
	}
}

function buildInterface()
{
	var canvas = document.getElementById('HelloWorld');
	stage = new Stage(canvas);
					
	stage.mouseEventsEnabled = true;
	stage.enableMouseOver();
	stage.addChild(bg);
	var mainScreen = new screen(stage);
	var levelScreen = new screen(stage);
	var gameScreen = new screen(stage);
	var levelVisual;
	for(var level = 0; level < 8; level++)
	{
		levelVisual = new visualObject(100+100*parseInt(level/2), 20+100*(level%2), 70, 70, 20, stage);
		levelVisual.text(level+1);
		levelVisual.nextScreen = gameScreen;
		levelScreen.addChild(levelVisual);
	}
	var gameVisual;
	for(var tile = 0; tile < 9; tile++)
	{
		gameVisual = new visualObject(150+100*parseInt(tile/3), 15+100*(tile%3), 70, 70, 20, stage);
		gameVisual.nextScreen = null;
		gameVisual.nextCommand = 
		gameScreen.addChild(gameVisual);
	}
	var back = new visualObject(150, 220, 270, 50, 20, stage);
	back.text("Back");
	back.nextScreen = mainScreen;
	levelScreen.addChild(back);
	var multi = new visualObject(150, 100, 270, 50, 20, stage);
	multi.text("Multiplayer")
	mainScreen.addChild(multi);
	var single = new visualObject(150, 170, 270, 50, 20, stage);
	single.text("Single Player")
	single.nextScreen = levelScreen;
	mainScreen.addChild(single);
	levelScreen.clearScreen();
	gameScreen.clearScreen();
	stage.update();
}


function screen(stage) 
{
	this.stage = stage;
	this.children = new Array();
	this.addChild = function (child) {
		child.screen = this;
		this.children.push(child);
	}
	
	this.clearScreen = function ()
	{
		for(var child in this.children)
		{
			//console.log(this.children);
			//console.log(this.children[child]);
			this.children[child].clear();
		}
	}
	
	this.showScreen = function ()
	{
		for(var child in this.children)
		{
			this.children[child].show();
		}
	}
}

function objectEngine(value, stage, visual, engine)
{
	this.stage = stage;
	this.value = value;
	this.visual = visual;
	this.engine = engine;
	this.changeColour = function (colour)
	{
		this.value.colour(colour);
	}
	this.changeValue() {
		this.engine.changeValue(this);
	}
	this.getValue()
	{
		return this.value;
	}
	this.setValue()
	{
		this.value = value;
	}
	
}

function mainEngine(stage, previous)
{
	this.stage = stage;
	this.previous = previous;
	this.children = new Array();
	function addChild(child)
	{
		this.children.push(child);
	}
	function changeValue(object)
	{
		object.setValue(object.getValue()+previous.getValue());
	}
}

function visualObject(locX, locY, sizeX, sizeY, radius, stage)
{
	this.stage = stage;
	this.locX = locX;
	this.locY = locY;
	this.sizeX = sizeX;
	this.sizeY = sizeY;
	this.radius = radius;
	this.screen;
	this.nextScreen;
	var graphics = new Graphics();
	graphics.beginFill(Graphics.getRGB(255,255,255,1));
	graphics.drawRoundRect(this.locX-1, this.locY-1, this.sizeX+2, this.sizeY+2, this.radius);
	this.highlightObject = new Shape(graphics);
	var graphics = new Graphics();
	graphics.beginFill(Graphics.getRGB(0,0,0,1));
	graphics.drawRoundRect(this.locX, this.locY, this.sizeX, this.sizeY, this.radius);
	this.shape = new Shape(graphics);
	this.stage.addChild(this.shape);
	console.log("Success");
	this.shape.mouseEnabled = true;
	this.colour = colour;
	this.text = text;
	this.shape.onMouseOver = function (mouseEvent)
	{
		this.stage.addChildAt(this.highlightObject, this.stage.getChildIndex(this.shape))
		//this.stage.removeChild(this.shape);
		//this.stage.addChild(this.highlightObject, this.shape);
		this.stage.update();
	}.bind(this)
	this.shape.onMouseOut = function (mouseEvent)
	{
		this.stage.removeChild(this.highlightObject);
		this.stage.update();
	}.bind(this)
	this.shape.onClick = function(mouseEvent)
	{
		if (nextScreen != null)
		{
			this.screen.clearScreen();
			this.nextScreen.showScreen();
		} else
		{
			this.nextCommand();
		}
		//this.colour(Graphics.getRGB(150, 255, 255, 1));
		//this.text("1");
	}.bind(this)
	this.label = new Text("", "40px Arial", "white");
	this.label.textAlign = "center";
	this.label.x = this.locX+this.sizeX/2;
	this.label.y = this.locY+this.sizeY/2+this.sizeY/4;
	this.label.maxWidth=this.sizeX;
	this.label.visible = false;
	stage.addChild(this.label);
	this.clear = clear
	
	
	function colour(colourSquare)
	{
		var graphics = new Graphics();
		graphics.beginFill(colourSquare);
		graphics.drawRoundRect(this.locX, this.locY, this.sizeX, this.sizeY, this.radius);
		this.shape.graphics = graphics;
		this.stage.update();
	}
	
	function clear()
	{
		this.shape.visible = false;
		this.label.visible = false;
		this.highlightObject.visible = false;
		stage.update();
	}
	
	this.show = function ()
	{
		this.shape.visible = true;
		this.label.visible = true;
		this.highlightObject.visible = true;
		stage.update();
	}
	
	function text(textData)
	{
		this.label.text = textData;
		this.label.visible = true;
		stage.update();
	}
}